//
//  ZCWebViewController.m
//  DoctorOnline
//
//  Created by 赵隆杰 on 2018/8/20.
//  Copyright © 2018年 赵隆杰. All rights reserved.
//

#import "ZCWebViewController.h"
#import "ZCWebView.h"

@interface ZCWebViewController ()<ZCWebViewDelegate>

@property (strong, nonatomic) ZCWebView *webView;

@end

@implementation ZCWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.centerTitle.text = self.titleName;
    self.naviView.hidden = self.isHiddenNavi;
    
    if (self.rightTitle) {
        self.rightBtn.frame = CGRectMake(SL_WINDOW_WIDTH - 100, UIDevice.vg_statusBarHeight, 100, UIDevice.vg_navigationBarHeight);
        [self.rightBtn setTitle:self.rightTitle forState:UIControlStateNormal];
    }
    
    [self loadWebView];
}

- (void)loadWebView {
    
    _webView = [[ZCWebView alloc] init];
//    _webView.scrollView.bounces = NO;

    _webView.zcWebDelegate = self;
    VHWeakSelf
    _webView.handleJSAtion = ^(NSInteger type, id data) {
        VHStrongSelf
        [self handleJSAction:type data:data];
    };
    
    [self.view addSubview:_webView];
    [_webView mas_makeConstraints:^(MASConstraintMaker *make) {
        if (!self.isHiddenNavi && !self.isClearNavi) {
            make.top.mas_equalTo(UIDevice.vg_navigationFullHeight);
        } else {
            make.top.equalTo(self.view);
        }
        make.left.right.bottom.equalTo(self.view);
    }];
    
    if (self.url) {
        
        NSURL *nsurl = [NSURL URLWithString:self.url];
        NSURLRequest *request = [[NSURLRequest alloc]initWithURL:nsurl cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:15];
        [_webView loadRequest:request];
        [self laterHideHUd];
    }
    
    if (self.isClearNavi) {
        self.naviView.backgroundColor = [UIColor clearColor];
        self.bottomLine.backgroundColor = [UIColor clearColor];
        [self.view bringSubviewToFront:self.naviView];
    }
}

// 有时候菊花 会一直消不掉 ，7s自动消失
-(void)laterHideHUd
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(7 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:self.webView animated:YES];
    });
}

/// JS事件
-(void)handleJSAction:(NSInteger)type data:(id)data
{
    //
    switch (type) {
            
        case 1://跳转新的h5
        {
            NSString *url = data[@"url"];
            NSString *title = data[@"title"];
            [[AppLogic sharedInstance] pushToWebViewVC:title url:H5AppendUrl(url) isClearNavi:YES];
        }
            break;
            
        default:
            break;
    }
    
    
}


#pragma mark - ZCWebViewDelegate
/// 开始加载
-(void)webViewStartLoad:(ZCWebView *)webView
{
    [MBProgressHUD showHUDAddedTo:self.webView animated:YES];
}
/// 加载完成
-(void)webViewLoadFinished:(ZCWebView *)webView
{
    [MBProgressHUD hideHUDForView:self.webView animated:YES];
    
//    if (webView.title.length > 0 && webView.canGoBack) {
//        self.topTitleLabel.text = webView.title;
//        self.currentTitle = webView.title;
//    } else {
//        self.topTitleLabel.text = self.titlename;
//        self.currentTitle = self.titlename;
//    }
//
//    self.currentURL = webView.URL.absoluteString;
//
//    // 能后退，说明跳了个新页面
//    if(webView.canGoBack){
//        self.closeBtn.hidden = NO;
//    }
}
/// 加载失败
-(void)webViewLoadFailed:(ZCWebView *)webView
{
    [MBProgressHUD hideHUDForView:self.webView animated:YES];
}

-(void)dealloc
{
    NSLog(@"release");
}


- (void)rightBtnClick
{
    [[AppLogic sharedInstance] pushToWebViewVC:self.rightTitle url:self.rightUrl];
}

@end
